<?php

namespace App\Http\Controllers\Wechat;

use App\Common\Response;
use App\Pull\Center;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    //
    private $appId;
    private $secret;
    public function __construct()
    {
        if (empty($config)){
            $config = config('wechat');
        }
        $this->appId = $config['appid'];
        $this->secret = $config['secret'];
    }

    /**
     * 获取code
     * @param $code
     * @return mixed
     * User: aniu
     * Date: 2020/6/6 4:12 下午
     */
    public function userAuthorize(Request $request, Response $response)
    {
        $code = $request->get('code', "null");
        $authCodeAndCode2session = new Center();
        $result = $authCodeAndCode2session->authCodeAndCode2session($code);
        return $response->jsonResponse(2000, [$result]);
    }

    /**
     * Notes:解析用户信息
     * User: aniu
     * Date: 2020/6/9
     * Time: 17:14
     * @param Request $request
     * @param Response $response
     * @return array|\Illuminate\Http\JsonResponse|string|\Symfony\Component\HttpFoundation\JsonResponse|null
     */
    public function getUserInfo(Request $request, Response $response)
    {
        $validator = Validator::make(
            $request->all(),[
                'sessionKey'  => 'required|string',
                'encryptedData' => 'required|string',
                'iv' => 'required|string'
            ]
        );
        if ($validator->fails()){
            $warnings = $validator->messages();
            $show_warning = $warnings->first();
            return $response->jsonResponseErr(3000, $show_warning);
        }
        $sessionKey = $request->input('sessionKey');
        $encryptedData = $request->input('encryptedData');
        $openid = $request->input('openid');
        $iv = $request->input('iv');
        $appid = $this->appId;
        $wxcrypt = new WXBizDataCrypt($appid, $sessionKey);
        $errCode = $wxcrypt->decryptData($encryptedData, $iv, $data);
        if ($errCode != 0)  return $response->jsonResponseErr(3000, $errCode);
        $phone = json_decode($data);
        $new_tel3 = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $phone->phoneNumber);
        $datas['username'] = $new_tel3;
        return $response -> jsonResponse(2000, $datas);
    }


    /**
     * Notes: 获取微信accesstoken
     * User: aniu
     * Date: 2020/6/16
     * Time: 9:18
     * @return mixed
     */
    public function get_accessToken()
    {
//        $accesstoken =Cache::get('accessToken');
//        if (empty($accesstoken)) {
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->secret; //替换成自己的小程序id和secret
            @$weixin = file_get_contents($url);
            @$jsondecode = json_decode($weixin);
            @$array = get_object_vars($jsondecode);
            $token = $array['access_token'];
//            Cache::put('accessToken', $token, 7200);  //添加获取微信token的缓存
//        }
        return $token;
    }
}
